package com.www.applyword.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;

import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import java.util.ArrayList;
import java.util.List;

/**
 * author : huang
 * date   : 2019/11/12 0012 18:01
 * version: 1.0
 * @author Administrator
 */
public class LocationActivity extends BaseActivity {   //LocationActiviy
    List<HotCity> hotCities = new ArrayList<>();
    //  hotCities.add(new HotCity("杭州", "浙江", "101210101"));


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.location_activity);
       // setTitle("切换城市");
    }

    @Override
    protected void loadData() {
        CityPicker.from(this)
                //启用动画效果，默认无
                .enableAnimation(true)
                //  .setAnimationStyle(anim)	//自定义动画
                //APP自身已定位的城市，传null会自动定位（默认）
                .setLocatedCity(null)
                //指定热门城市
                .setHotCities(hotCities)
                .setOnPickListener(new OnPickListener() {
                    @Override
                    public void onPick(int position, City data) {
                       // Utils.showToastSafe(data.getName());
                        Intent intent = new Intent();
                        if (!TextUtils.isEmpty(data.getName())){
                            intent.putExtra("city", data.getName());
                        }else {
                            intent.putExtra("city", "武汉");
                        }

                        // 设置返回码和返回携带的数据
                        setResult(Activity.RESULT_OK, intent);
                        // RESULT_OK就是一个默认值，=-1，它说OK就OK吧
                        finish();
                        //   Toast.makeText(getApplicationContext(), data.getName(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel() {
                        finish();
                        //   Utils.showToastSafe("取消选择");
                        //  Toast.makeText(getApplicationContext(), "取消选择", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onLocate() {
                        //定位接口，需要APP自身实现，这里模拟一下定位
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //定位完成之后更新数据
                                CityPicker.from(LocationActivity.this).locateComplete(new LocatedCity("武汉", "湖北", "101280601"), LocateState.SUCCESS);
                            }
                        }, 3000);
                    }
                })
                .show();
    }
}
